<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>栏目管理</title>
<link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css">
<script type="text/javascript" src="/js/easyui/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
<script>
    function createColumn() {
        $("#columnForm").form('submit', {
            url: "/management/columns/columnForm.action",
            onSubmit:function() {
                return $(this).form('validate');
            },
            success:function(data) {
                //   alert(data);
                data = eval('(' + data + ')');
                var columns = data.columns;

                if (!data.successful) {
                    alert('not true');
                    return;
                }

                $.messager.show({
                    title:'提示信息',
                    msg:'保存成功！'
                });

                if (!data.edit) {
                    if ("" == columns[0].parentId) {
                        $('#columnsGrid').treegrid('append', {
                            parent: null,
                            data: columns
                        });
                    } else {
                        var node = $('#columnsGrid').treegrid('getSelected');
                        $('#columnsGrid').treegrid('append', {
                            parent: node.id,
                            data: columns
                        });
                    }

                } else {
                    var node = $('#columnsGrid').treegrid('getSelected');
                    node.name = columns[0].name;
                    node.url = columns[0].url;
                    node.title = columns[0].title;
                    node.keywords = columns[0].keywords;
                    node.description = columns[0].description;
                    $('#columnsGrid').treegrid('refresh', node.id);
                }

                closeColumnForm();
            }
        });
    }

    function deleteColumn() {
        var node = $('#columnsGrid').treegrid('getSelected');
        var rows = $('#columnsGrid').treegrid('getSelections');

        if (rows.length != 1) {
            $.messager.alert('警告', '请选择一条记录!', 'warning');
            return;
        }

        $.messager.confirm('删除栏目', '你确定要删除栏目: '+node.name+'吗？', function(data) {
            if (!data) {
                return;
            }

            $.getJSON("/management/columns/deleteColumn.action?id=" + node.id, function(data) {
                if (data) {
                    $.messager.show({
                        title:'提示信息',
                        msg:'删除成功！'
                    });
                    $('#columnsGrid').treegrid('remove', node.id);
                }
            });
        });
    }

    function closeColumnForm() {
        $('#itemEditorDialog').dialog('close');
    }

    function addRootColumn() {
        $('#columnForm').form('clear');
        $('#itemEditorDialog').show();
        $('#itemEditorDialog').dialog({
            title:'添加栏目',
            modal:true
        });
    }

    function addColumn() {
        var node = $('#columnsGrid').treegrid('getSelected');
        var rows = $('#columnsGrid').treegrid('getSelections');

        if (rows.length != 1) {
            $.messager.alert('警告', '请选择一条记录!', 'warning');
            return;
        }

        $('#columnForm').form('clear');
        $('#columnForm').form('load', {
            'columnForm.parentId':node.id
        });

        $('#itemEditorDialog').show();
        $('#itemEditorDialog').dialog({
            title:'添加栏目',
            modal:true
        });
    }

    function editColumn() {
        var node = $('#columnsGrid').treegrid('getSelected');
        var rows = $('#columnsGrid').treegrid('getSelections');

        if (rows.length != 1) {
            $.messager.alert('警告', '请选择一条记录!', 'warning');
            return;
        }

        $('#columnForm').form('load', {
            'columnForm.parentId':node.parentId,
            'columnForm.id':node.id,
            'columnForm.name':node.name,
            'columnForm.url':node.url,
            'columnForm.title':node.title,
            'columnForm.keywords':node.keywords,
            'columnForm.description':node.description,
            'columnForm.createDateTime':node.createDateTime
        });

        $('#itemEditorDialog').show();
        $('#itemEditorDialog').dialog({
            title:'编辑栏目',
            modal:true
        });
    }

    $(function() {
        $('#columnsGrid').treegrid({
            title:'栏目管理',
            width:1340,
            height:620,
            rownumbers: true,
            url:'/management/columns/fetchColumns.action',
            idField:'id',
            treeField:'name',
            fitColumns: true,
            columns:[
                [
                    {field:'name',title:'名称',width:160},
                    {field:'url',title:'URL',width:120},
                    {field:'title',title:'Title',width:660},
                    {field:'keywords',title:'keywords',width:320},
                    {field:'description',title:'description',width:320},
                    {field:'createDateTime',title:'创建日期',width:120}
                ]
            ],
            toolbar:[
                {
                    text:'添加一级栏目',
                    iconCls:'icon-add',
                    handler:function() {
                        addRootColumn();
                    }
                },
                {
                    text:'添加子栏目',
                    iconCls:'icon-add',
                    handler:function() {
                        addColumn();
                    }
                },
                {
                    text:'编辑栏目',
                    iconCls:'icon-save',
                    handler:function() {
                        editColumn();
                    }
                },
                '-',
                {
                    text:'删除栏目',
                    iconCls:'icon-cut',
                    handler:function() {
                        deleteColumn();
                    }
                }
            ]
        });
    });

    function reload() {
        var node = $('#columnsGrid').treegrid('getSelected');
        if (node) {
            $('#test').treegrid('reload', node.code);
        } else {
            $('#test').treegrid('reload');
        }
    }

</script>
</head>
<body>
<%--<h1>栏目管理</h1>--%>
<s:if test="null == #session._login_user_">
    <script type="text/javascript">
        top.location.href = '/admin';
    </script>
</s:if>

<table id="columnsGrid"></table>

<div id="itemEditorDialog" icon="icon-save" title="栏目管理" style="height:400;width:400;display:none;padding:5px;">
    <form method="post" id="columnForm" name="columnForm">
        <table>
            <tr>
                <td><label>栏目名称：</label></td>
                <td><input class="easyui-validatebox" id="name" name="columnForm.name" required="true"/></td>
            </tr>
            <tr>
                <td><label>栏目url：</label></td>
                <td><input class="easyui-validatebox" id="url" name="columnForm.url"/></td>
            </tr>
             <tr>
                <td><label>栏目title：</label></td>
                <td><input id="title" name="columnForm.title" style="width: 540px"/></td>
            </tr>
            <tr>
                <td><label>栏目keywords：</label></td>
                <td><input id="keywords" name="columnForm.keywords" style="width: 540px"/></td>
            </tr>
            <tr>
                <td><label>栏目description：</label></td>
                <td><input id="description" name="columnForm.description" style="width: 540px"/></td>
            </tr>
        </table>
        <div class="toolbar" style="padding:10px;">
            <a href="#" onclick="closeColumnForm()" class="easyui-linkbutton" iconCls="icon-cancel" style="float: right;">取消</a>
            <a href="#" onclick="createColumn()" class="easyui-linkbutton" iconCls="icon-ok" style="float: right;">确定</a>
        </div>
        <input type="hidden" name="columnForm.parentId"/>
        <input type="hidden" name="columnForm.id"/>
        <input type="hidden" name="columnForm.createDateTime"/>
    </form>
</div>

</body>
</html>